package com.teamone.etl.accountant.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.accountant.entity.Accountant;
import com.teamone.etl.accountant.entity.Finance;
import com.teamone.etl.accountant.entity.RechargeSheet;
import com.teamone.etl.accountant.service.AccountantService;
import com.teamone.etl.accountant.service.FinanceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 财务管理(Finance)表控制层
 *
 * @author makejava
 * @since 2021-01-21 11:58:11
 */
@RestController
@RequestMapping("/finance")
public class FinanceController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private FinanceService financeService;
    @Resource
    private AccountantService accountantService;

    /**
     * 订单列表模糊+分页查询
     * @param rechargeId    订单编号
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param page          当前页
     * @param size          每页条数
     * @return              部分数据
     */
    @GetMapping("/financeSearch")
    public R financeSearch(@Param("rechargeId") String rechargeId,
                           @RequestParam(value = "startTime",defaultValue = "1392-01-01 00:00:00") String startTime,
                           @RequestParam(value = "endTime",defaultValue = "2299-12-30 00:00:00") String endTime,
                           @RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "size",defaultValue = "10") Integer size) {
        PageInfo<RechargeSheet> search = financeService.financeSearch(rechargeId, startTime, endTime, page, size);
        return success(search);
    }

    /**
     *  待审核订单
     * @param page  当前页
     * @param size  每条数据
     * @return      每条数据
     */
    @GetMapping("/orderReview")
    public R orderReview(@RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        PageInfo<Finance> review = financeService.orderReview(page, size);
        return success(review);
    }

    /**
     *  审核通过订单/审核不通过订单
     * @param id    主键
     * @param flag  1:通过 2:不通过
     * @return      是否成功
     */
    @PutMapping("/reviewPass/{id}/{flag}")
    public R reviewPass(@PathVariable("id") Integer id,@PathVariable("flag") Integer flag){
        return success(financeService.reviewPass(id,flag));
    }

    /**
     * 财务统计
     * @return 每个月的总金额
     */
    @GetMapping("/accountStatics")
    public R accountStatics(){
        return success(financeService.accountStatics());
    }

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param finance 查询实体
     * @return 所有数据
     */
    @GetMapping("")
    public R selectAll(Page<Finance> page, Finance finance) {
        return success(this.financeService.page(page, new QueryWrapper<>(finance)));
    }

    /**
     * 账户管理
     * @return 全部数据
     */
    @GetMapping("/accountManager")
    public R accountManager(){
        return success(accountantService.list());
    }

    /**
     * 修改账户数据
     *
     * @param accountant 实体对象
     * @return 修改结果
     */
    @PutMapping("/accountUpdate")
    public R update(@RequestBody Accountant accountant) {
        return success(accountantService.updateById(accountant));
    }

    /**
     * 发票审核列表
     * @return 全部数据
     */
    @GetMapping("/auditInvoice")
    public R auditInvoice(){
        return success(financeService.auditInvoice());
    }

    /**
     * 发票详情
     * @param id 主键
     * @return
     * 部分数据
     */
    @GetMapping("/invoiceDetails/{id}")
    public R invoiceDetails(@PathVariable("id") Integer id){
        return success(financeService.invoiceDetails(id));
    }

    /**
     * 审核发票
     * @param id    主键
     * @param flag  更改状态值
     * @return      是否成功
     */
    @PostMapping("/reviewInvoice/{id}/{flag}")
    public R reviewInvoice(@PathVariable("id") Integer id,@PathVariable("flag") Integer flag){
        return success(financeService.reviewInvoice(id,flag));
    }

}